#undef UNICODE
#include "InputMessage.h"
#include <stdio.h>
#include <stdlib.h>

//void InputMessage::pack(char* buffer)
//{
//	//condense bools
//	//all 256 bools for keyboard state are packed into 4 "unsinged long longs"
//	for(int i=0;i<NUM_BOOL_STORAGE_VARS;++i)
//		storage[i] = 0;
//	int cursor=0;
//
//	for(int i=0;i<NUM_BOOL_STORAGE_VARS;++i)
//		for(int j=0;j<(sizeof(BOOL_STORAGE))*BITS_PER_BYTE;++j, cursor++)
//				storage[i] |= (data.keyBoard[cursor] << j);
//
//	//clicked bool is positive for true and negative for false
//	int clicked = -1;
//	if(data.mouseClicked)
//		clicked = 1;
//	//X#Y#clicked#A#B#C#D#...
//	sprintf_s(buffer, MESSAGE_BUFF_LEN*sizeof(char), "%g#%g#%i#%lu#%lu#%lu#%lu#",
//		data.mousePos.x, data.mousePos.y, clicked, storage[0],storage[1],storage[2],storage[3]);
//}

void InputMessage::pack(char* buffer)
{
	//bool is positive for true and negative for false
	int w = -1,a = -1,s = -1,d = -1,spacebar = -1;
	if(data.keyBoard[DIK_W] || data.keyBoard[DIK_UP])
		w = 1;
	if(data.keyBoard[DIK_A] || data.keyBoard[DIK_LEFT])
		a = 1;
	if(data.keyBoard[DIK_S] || data.keyBoard[DIK_DOWN])
		s = 1;
	if(data.keyBoard[DIK_D] || data.keyBoard[DIK_RIGHT])
		d = 1;
	if(data.keyBoard[DIK_SPACE])
		spacebar = 1;

	//clicked bool is positive for true and negative for false
	int clicked = -1;
	if(data.mouseClicked)
		clicked = 1;
	//X#Y#clicked#A#B#C#D#...
	sprintf_s(buffer, MESSAGE_BUFF_LEN*sizeof(char), "%g#%g#%i#%i#%i#%i#%i#%i#",
		data.mousePos.x, data.mousePos.y, clicked, w, a, s, d, spacebar);
}

void InputMessage::unPack(char* dataBuffer)
{
	char end = '\0', *p = &end;
	int counter=0;

	if(dataBuffer && dataBuffer != (char*)0xfefefefe)
	{

		//MousePos
		counter += ParseString(counter, temp, dataBuffer, stop);
		data.mousePos.x = (float)atof(temp);
		counter += ParseString(counter, temp, dataBuffer, stop);
		data.mousePos.y = (float)atof(temp);

		//MouseClicked
		int clicked;
		counter += ParseString(counter, temp, dataBuffer, stop);
		clicked = strtol(temp, &p, 10);
		data.mouseClicked = false;
		if(clicked > 0)
			data.mouseClicked = true;

		//Keyboard
		int w;
		counter += ParseString(counter, temp, dataBuffer, stop);
		w = strtol(temp, &p, 10);
		data.keyBoard[DIK_W] = false;
		if(w > 0)
			data.keyBoard[DIK_W] = true;
	
		int a;
		counter += ParseString(counter, temp, dataBuffer, stop);
		a = strtol(temp, &p, 10);
		data.keyBoard[DIK_A] = false;
		if(a > 0)
			data.keyBoard[DIK_A] = true;
	
		int s;
		counter += ParseString(counter, temp, dataBuffer, stop);
		s = strtol(temp, &p, 10);
		data.keyBoard[DIK_S] = false;
		if(s > 0)
			data.keyBoard[DIK_S] = true;
	
		int d;
		counter += ParseString(counter, temp, dataBuffer, stop);
		d = strtol(temp, &p, 10);
		data.keyBoard[DIK_D] = false;
		if(d > 0)
			data.keyBoard[DIK_D] = true;
	
		int spacebar;
		counter += ParseString(counter, temp, dataBuffer, stop);
		spacebar = strtol(temp, &p, 10);
		data.keyBoard[DIK_SPACE] = false;
		if(spacebar > 0)
			data.keyBoard[DIK_SPACE] = true;
	}
}

//void InputMessage::unPack(char* dataBuffer)
//{
//	char stop = DELIMITER, *p = &stop , temp[MESSAGE_BUFF_LEN];
//	int counter=0;
//
//	//MousePos
//	counter += ParseString(counter, temp, dataBuffer, stop);
//	data.mousePos.x = (float)atof(temp);
//	counter += ParseString(counter, temp, dataBuffer, stop);
//	data.mousePos.y = (float)atof(temp);
//
//	//MouseClicked
//	int clicked;
//	counter += ParseString(counter, temp, dataBuffer, stop);
//	clicked = strtol(temp, &p, 10);
//	data.mouseClicked = false;
//	if(clicked > 0)
//		data.mouseClicked = true;
//
//	//Keyboard
//	for(int i=0;i<NUM_BOOL_STORAGE_VARS;++i)
//	{
//		counter += ParseString(counter, temp, dataBuffer, stop);
//		storage[i] = StringToBoolStorageType(temp);
//	}
//
//	//extract bools
//	int cursor=0;
//	for(int i=0;i<NUM_BOOL_STORAGE_VARS;++i)
//	{
//		cursor = i*sizeof(BOOL_STORAGE)*BITS_PER_BYTE;
//		if(storage[i])
//			for(int j=0;j<(sizeof(BOOL_STORAGE))*BITS_PER_BYTE;++j, cursor++)
//				data.keyBoard[cursor] = (storage[i] & (1ULL << j));
//	}
//}